import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFrame;


public class MouseTest {


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				MouseFrame frame = new MouseFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});

	}

}

class MouseFrame extends JFrame {
	public MouseFrame() {
		setTitle("MouseTest");
		setSize(300,200);
		
		add(new MouseComponent());
	}
}


class MouseComponent extends JComponent {
	
	private static final int SIDE = 10;
	private ArrayList<Rectangle2D> squares;
	private Rectangle2D current;
	
	
	public MouseComponent() {
		squares = new ArrayList<Rectangle2D>();
		current = null;
		
		addMouseListener(new MouseHandler());
		addMouseMotionListener(new MouseMotionHandler());
	}
	
	public void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		
		for(Rectangle2D r : squares) {
			g2.draw(r);
		}
	}
	
	public Rectangle2D find(Point2D p) {
		for(Rectangle2D r : squares) {
			if(r.contains(p)) return r;
		}
		return null;
	}
	
	public void add(Point2D p) {
		double x = p.getX();
		double y = p.getY();
		
		current = new Rectangle2D.Double(x-SIDE/2, y-SIDE/2, SIDE, SIDE);
		squares.add(current);
		repaint();
	}
	
	public void remove(Rectangle2D s) {
		if(s == null) return;
		if(s == current) current = null;
		squares.remove(s);
		repaint();
	}
	
	
	private class MouseHandler extends MouseAdapter {
		public void mousePressed(MouseEvent event) {
			current = find(event.getPoint());
			if(current == null) add(event.getPoint());
		}
		
		public void mouseClicked(MouseEvent event) {
			current = find(event.getPoint());
			if(current != null && event.getClickCount() >= 2) {
				remove(current);
			}
		}
	}
	
	
	private class MouseMotionHandler implements MouseMotionListener {

		@Override
		public void mouseDragged(MouseEvent e) {
			if(current != null) {
				int x = e.getX();
				int y = e.getY();
				
				current.setFrame(x - SIDE, y - SIDE, SIDE, SIDE);
				repaint();
			}
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			if((find(e.getPoint())) == null) {
				setCursor(Cursor.getDefaultCursor());
			} else {
				setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
			}
		}
		
	}
}